package utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.NetworkOnMainThreadException;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.picture_sharing_system.R;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MyWorksListAdapter
        extends RecyclerView.Adapter<MyWorksListAdapter.ViewHolder> {

    private List<Works> mWorksData;
    private Context mContext;
    private int resourceId;

    public MyWorksListAdapter(Context context, int resourceId, List<Works> data) {
        this.mContext = context;
        this.mWorksData = data;
        this.resourceId = resourceId;
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext)
                .inflate(resourceId, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tv_my_username.setText(mWorksData.get(position).getUsername());
        holder.tv_my_msg.setText(mWorksData.get(position).getMessage());


        byte[] bytes = Base64.decode(mWorksData.get(position).getPictureBase64(), Base64.DEFAULT);
        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        holder.iv_my_Image.setImageBitmap(bitmap);

        holder.numOfStars.setText(mWorksData.get(position).getNumOfStars() + " 喜欢");
        holder.tv_time.setText(mWorksData.get(position).getcTime());
        holder.my_work_id.setText(mWorksData.get(position).getId());
        String workid = holder.my_work_id.getText().toString();


        holder.btn_remove_work.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Request request = new Request.Builder()
                                .url(Constants.REMOVE_WORK_URL + "?workid=" + workid)
                                .get().build();
                        try {
                            OkHttpClient client = new OkHttpClient();
                            client.newCall(request).enqueue(new Callback() {
                                @Override
                                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                                }

                                @Override
                                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                }
                            });
                        } catch (NetworkOnMainThreadException ex) {
                            ex.printStackTrace();
                        }
                    }
                }).start();

                for (Works works : mWorksData) {
                    if (works.getId().equals(workid)) {
                        mWorksData.remove(works);
                        notifyDataSetChanged();
                        break;
                    }
                }
                Toast.makeText(v.getContext(), "删除成功", Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public int getItemCount() {
        return mWorksData.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv_my_username;
        TextView tv_my_msg;
        ImageView iv_my_Image;
        TextView tv_time;
        Button btn_remove_work;
        TextView my_work_id;
        TextView numOfStars;

        public ViewHolder(View view) {
            super(view);

            my_work_id = view.findViewById(R.id.myworkid);
            btn_remove_work = view.findViewById(R.id.btn_remove_work);
            tv_my_username = view.findViewById(R.id.tv_my_works_username);
            tv_my_msg = view.findViewById(R.id.tv_my_works_msg);
            iv_my_Image = view.findViewById(R.id.iv_my_works_picture);
            tv_time = view.findViewById(R.id.tv_my_works_cTime);
            numOfStars = view.findViewById(R.id.numOfStars);

        }
    }
}